// 公共属性
export default class Feature {
  static apply(object) {
    object._dispose = Feature.prototype._dispose
    object._disposeHierarchy = Feature.prototype._disposeHierarchy
    object._destroyWorker = Feature.prototype._destroyWorker
  }

  _destroyWorker(viewer, worker) {
    worker.terminate()
    viewer.workerQueue.workingNum -= 1
    viewer.workerQueue.changeStatus()
  }

  _disposeHierarchy(node, callback) {
    if (!node) return
    if (!node.children) {
      callback(node)
    } else {
      for (var i = node.children.length - 1; i >= 0; i--) {
        var child = node.children[i]
        this._disposeHierarchy(child, callback)
        callback(child)
      }
    }
  }

  _dispose(e) {
    if (e.geometry) {
      e.geometry.dispose()
    }
    if (e.material) {
      if (e.material instanceof THREE.MeshFaceMaterial || e.material.type === 'MeshFaceMaterial') {
        e.material.materials.forEach((material) => {
          if (material.map) material.map.dispose()
          if (material.lightMap) material.lightMap.dispose()
          if (material.bumpMap) material.bumpMap.dispose()
          if (material.normalMap) material.normalMap.dispose()
          if (material.specularMap) material.specularMap.dispose()
          if (material.envMap) material.envMap.dispose()
          material.dispose()
        })
      } else {
        if (e.material.map) e.material.map.dispose()
        if (e.material.lightMap) e.material.lightMap.dispose()
        if (e.material.bumpMap) e.material.bumpMap.dispose()
        if (e.material.normalMap) e.material.normalMap.dispose()
        if (e.material.specularMap) e.material.specularMap.dispose()
        if (e.material.envMap) e.material.envMap.dispose()
        e.material.dispose()
      }
    }
    if (e.dispose) {
      e.dispose()
    }
    e.parent.remove(e)
  }
}
